import {
  CartModel
} from '../../models/cart.js';

const cartModel = new CartModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList: [],
    cartTotal: {
      goodsCount: 0,
      goodsAmount: 0.00,
      checkedGoodsCount: 0,
      checkedGoodsAmount: 0.00
    },
    isEditCart: false,
    checkedAllStatus: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  
  onShow: function () {
    // 页面显示
    this.getCartList();
  },

  getCartList: function() {
    cartModel.getCartList().then(res => {
      // console.log(res);
      if (res.errno === 0) {
        this.setData({
          cartList: res.data.cartList,
          cartTotal: res.data.cartTotal
        })
      }

      this.setData({
        checkedAllStatus: this.isCheckedAll()
      });
    })
  },

  // 判断购物车商品是否全选
  isCheckedAll: function () {
    return this.data.cartList.every((element, index, array) => {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });
  },

  // 获取购购车选中商品的数量（没有用到）
  getCheckedGoodsCount: function(){
    let checkedGoodsCount = 0;
    this.data.cartGoods.forEach(function (v) {
      if (v.checked === true) {
        checkedGoodsCount += v.number;
      }
    });
    console.log(checkedGoodsCount);
    return checkedGoodsCount;
  },

  // 点击选项按钮
  checkedItem: function(event) {
    let itemIndex = event.currentTarget.dataset.itemIndex;

    let p_productIds = this.data.cartList[itemIndex].product_id;
    let p_isChecked = this.data.cartList[itemIndex].checked ? 0 : 1;
    cartModel.getCartChecked(p_productIds, p_isChecked).then(res => {
      if (res.errno === 0) {
        this.setData({
          cartList: res.data.cartList,
          cartTotal: res.data.cartTotal
        });
      }
    
      this.setData({
        checkedAllStatus: this.isCheckedAll()
      });
    })
  },

  // 点击 “全选” 按钮
  checkedAll: function () {
    let p_productIds = this.data.cartList.map(v => {
      return v.product_id;
    });

    cartModel.getCartChecked(p_productIds.join(','), this.isCheckedAll() ? 0 : 1).then(res => {
      if (res.errno === 0) {
        this.setData({
          cartList: res.data.cartList,
          cartTotal: res.data.cartTotal
        });
      }

      this.setData({
        checkedAllStatus: this.isCheckedAll()
      });
    })
  },

  // 点击 “编辑” 按钮
  editCart: function() {
    if (!this.data.isEditCart) {
      //非编辑状态
      this.setData({
        isEditCart: true
      });
    }
    else {
      //编辑状态
      let tmpCartList = this.data.cartList.map(function (v) {
        v.checked = false;
        return v;
      });
      
      this.setData({
        isEditCart: false
      });
      
      this.getCartList();
    }
  },

  // 更新购物车
  updateCart: function (p_productId, p_goodsId, p_number, p_id) {
    cartModel.getCartUpdate(p_productId, p_goodsId, p_number, p_id).then(res => {
      if (res.errno === 0) {
        this.setData({
          checkedAllStatus: this.isCheckedAll()
        });
      }
    })
  },

  // 数量 -1
  cutNumber: function(event) {
    let itemIndex = event.target.dataset.itemIndex;
    let cartItem = this.data.cartList[itemIndex];
    let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
    cartItem.number = number;
    this.setData({
      cartList: this.data.cartList
    });
    
    this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  },

  // 数量 +1
  addNumber: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    let cartItem = this.data.cartList[itemIndex];
    let number = cartItem.number + 1;
    cartItem.number = number;
    console.log(cartItem.number);
    this.setData({
      cartList: this.data.cartList
    });
    this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  },

  // 点击 “下单” 按钮
  checkoutOrder: function() {
    let checkedGoods = this.data.cartList.filter((element, index, array) => {
      if (element.checked == true) {
        return true;
      }
      else {
        return false;
      }
    })

    if (checkedGoods.length <= 0) {
      wx.showModal({
        title: '温馨提示',
        content: '您还没有选择任何商品',
        showCancel: false
      })
      return false;
    }

    wx.navigateTo({
      url: '../shopping/checkout/index'
    })
  },

  // 点击 “删除所选” 按钮
  deleteCart: function() {
    let productIds = this.data.cartList.filter((element, index, array) => {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });

    if (productIds.length <= 0) {
      return false;
    }

    productIds = productIds.map(function (element, index, array) {
      if (element.checked == true) {
        return element.product_id;
      }
    });

    cartModel.getCartDelete(productIds.join(',')).then(res => {
      if (res.errno === 0) {
        let cartList = res.data.cartList.map(v => {
          console.log(v);
          v.checked = false;
          return v;
        });

        this.setData({
          cartList: cartList,
          cartTotal: res.data.cartTotal
        });
      }

      wx.showToast({
        title: '删除成功'
      })

      this.setData({
        checkedAllStatus: this.isCheckedAll()
      });
    })
  }
})